import {
  uploadToQiniu
} from "../../utils/uploadToQiniu"
Page({

  data: {
    img: '',
    name: '',
    phonenumber: '',
    has_img: '',
    ali_acc: '',
    is_ali_acc: true
  },


  onLoad(options) {
    this.setData({
      img: wx.getStorageSync('userInfo').img,
      name: wx.getStorageSync('userInfo').name,
      phonenumber: Number(wx.getStorageSync('userInfo').phonenumber),
      ali_acc: wx.getStorageSync('userInfo').ali_acc,
    })
    this.get_is_ali_acc()
    if (wx.onNeedPrivacyAuthorization) {
      wx.onNeedPrivacyAuthorization()
    }
  },

  get_is_ali_acc() {
    getApp().globalData.post({
      url: '/api/ouyoung/ali_acc_tab'
    }, true).then(res => {
      let data = res.data.data
      if (data.ali_acc_1 || data.ali_acc_2) {
        this.setData({
          is_ali_acc: data.ali_acc_1
        })
      }
    })
  },


  onChooseAvatar(e) {
    uploadToQiniu(e.detail.avatarUrl).then(src => {
      this.setData({
        img: src
      })
    })
  },

  change_name(e) {
    this.setData({
      name: e.detail.value
    })
  },

  change_ali_acc(e) {
    this.setData({
      ali_acc: e.detail.value
    })
  },

  getPhoneNumber(e) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      getApp().globalData.post({
        url: '/api/applet.login/getphonenumber.html',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        }
      }, true).then(r => {
        let data = r.data.data
        wx.setStorageSync("userInfo", data.userinfo);
        this.setData({
          phonenumber: data.phonenumber
        })
      })
    }
  },

  bottomTap() {
    if (!this.data.img) {
      getApp().showToast('请先选择头像')
      return
    }
    console.log(this.data.name, '333');
    if (!this.data.name) {
      getApp().showToast('请输入昵称')
      return
    }
    getApp().updwxinfo({
      name: this.data.name,
      img: this.data.img,
      phone: this.data.phonenumber,
      // ali_acc: this.data.ali_acc
    }).then(() => {
      if (this.data.phonenumber == 0 || this.data.phonenumber == '') {
        wx.redirectTo({
          url: './login'
        })
      } else {
        wx.navigateBack()
      }
    })
  },

})